<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="120px" label-position="left">
			<el-form-item label="合同标题" prop="title">
				<el-input v-model="form.title" placeholder="合同标题" maxlength="40" clearable></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="合同编号" prop="contractCode">
						<el-input v-model="form.contractCode" placeholder="合同编号" maxlength="40" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="合同类型" prop="contractType">
						<sc-select v-model="form.contractType" :params="{code:'contractType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="履行期限(起)" prop="startTime">
						<el-date-picker v-model="form.startTime" placeholder="履行期限(起)" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="履行期限(止)" prop="endTime">
						<el-date-picker v-model="form.endTime" placeholder="履行期限(止)" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="结算方式" prop="payType">
						<sc-select v-model="form.payType" :params="{code:'payType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="结算币种" prop="cashType">
						<sc-select v-model="form.cashType" :params="{code:'cashType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="合同金额" prop="total">
						<el-input-number v-model="form.total" placeholder="合同金额" clearable style="width:80%"></el-input-number>/元
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="已收金额" prop="realTotal">
						<el-input-number v-model="form.realTotal" placeholder="合同金额" clearable style="width:80%"></el-input-number>/元
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="签订时间" prop="signTime">
						<el-date-picker v-model="form.signTime" placeholder="签订时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="签订地址" prop="signAddress">
						<el-input v-model="form.signAddress" placeholder="签订地址" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="对方名称" prop="customerName">
						<el-input v-model="form.customerName" placeholder="对方名称" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="我方名称" prop="myOrgName">
						<el-input v-model="form.myOrgName" placeholder="我方名称" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="对方地址" prop="registerAddr">
						<el-input v-model="form.registerAddr" placeholder="对方地址" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="我方地址" prop="myOrgAdd">
						<el-input v-model="form.myOrgAdd" placeholder="我方地址" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="对方法人" prop="legalPerson">
						<el-input v-model="form.legalPerson" placeholder="对方法人" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="我方法人" prop="myLegalPerson">
						<el-input v-model="form.myLegalPerson" placeholder="我方法人" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="对方签订人" prop="customerSignUser">
						<el-input v-model="form.customerSignUser" placeholder="对方签订人" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="我方签订人" prop="mySignUser">
						<el-input v-model="form.mySignUser" placeholder="我方签订人" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="联系电话" prop="mobile">
						<el-input v-model="form.mobile" placeholder="联系电话" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="我方联系电话" prop="mySignMobile">
						<el-input v-model="form.mySignMobile" placeholder="我方联系电话" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="对方开户行" prop="bank">
						<el-input v-model="form.bank" placeholder="对方开户行" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="我方开户行" prop="myBank">
						<el-input v-model="form.myBank" placeholder="我方开户行" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="对方账号" prop="bankAccount">
						<el-input v-model="form.bankAccount" placeholder="对方账号" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="我方账号" prop="myBankAccount">
						<el-input v-model="form.myBankAccount" placeholder="我方账号" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="对方税号" prop="taxNo">
						<el-input v-model="form.taxNo" placeholder="对方税号" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="我方税号" prop="myTaxNo">
						<el-input v-model="form.myTaxNo" placeholder="我方税号" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-form-item label="合同说明">
				<sc-editor v-model="form.content" placeholder="请输入" :height="400" clearable style="width: 100%;"></sc-editor>
			</el-form-item>
			<el-form-item label="合同附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'fixedattach'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import {defineAsyncComponent} from "vue";
import AttachMenu from '@/views/module/attachMenu'
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog,
		AttachMenu,
		scEditor
	},
	data() {
		return {
			apiObj: this.$API.file.attach.uploadFile,
			fileList: [],
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			titleMap: {
				add: '合同登记对话框',
				edit: '合同编辑对话框',
			},
			form: {
				contractId:'',
				sortId:'',
				title: '',
				contractCode: '',
				contractType: '',
				startTime: '',
				endTime: '',
				payType: '',
				cashType: '',
				total: 0,
				realTotal: 0,
				signTime: '',
				signAddress: '',
				customerName: '',
				myOrgName: '',
				registerAddr: '',
				myOrgAdd: '',
				legalPerson: '',
				myLegalPerson: '',
				customerSignUser: '',
				mySignUser: '',
				mobile: '',
				mySignMobile: '',
				bank: '',
				myBank: '',
				bankAccount: '',
				myBankAccount: '',
				taxNo: '',
				myTaxNo: '',
				content: '',
				accountId: ''
			},
			rules: {
				title: [
					{required: true, message: '合同名称不能为空', trigger: 'blur'}
				],
				contractType:[
					{required: true, message: '合同类型不能为空', trigger: 'blur'}
				],
				contractCode: [
					{required: true, message: '合同编号不能为空', trigger: 'blur'}
				],
				total: [
					{required: true, message: '合同总额不能为空', trigger: 'blur'}
				],
				customerName:[
					{required: true, message: '对方名称不能为空', trigger: 'blur'}
				],
				startTime: [
					{required: true, message: '合同起始时间不能为空', trigger: 'blur'}
				],
				endTime: [
					{required: true, message: '合同终止时间不能为空', trigger: 'blur'}
				],
				signTime:[
					{required: true, message: '签定时间不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.finance.contract.insertContract.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.finance.contract.updateContract.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			if(this.mode=="add")
			{
				this.form.sortId = data.sortId
			}else {
				this.form.contractId = data.contractId
				this.form.assetsId = data.assetsId
				this.form.sortId = data.sortId
				this.form.title = data.title
				this.form.contractCode = data.contractCode
				this.form.contractType = data.contractType
				this.form.startTime = data.startTime
				this.form.endTime = data.endTime
				this.form.payType = data.payType
				this.form.cashType = data.cashType
				this.form.total = data.total
				this.form.realTotal = data.realTotal
				this.form.signTime = data.signTime
				this.form.signAddress = data.signAddress
				this.form.customerName = data.customerName
				this.form.myOrgName = data.myOrgName
				this.form.registerAddr = data.registerAddr
				this.form.myOrgAdd = data.myOrgAdd
				this.form.legalPerson = data.legalPerson
				this.form.myLegalPerson = data.myLegalPerson
				this.form.customerSignUser = data.customerSignUser
				this.form.mySignUser = data.mySignUser
				this.form.mobile = data.mobile
				this.form.mySignMobile = data.mySignMobile
				this.form.bank = data.bank
				this.form.myBank = data.myBank
				this.form.bankAccount = data.bankAccount
				this.form.myBankAccount = data.myBankAccount
				this.form.taxNo = data.taxNo
				this.form.myTaxNo = data.myTaxNo
				this.form.content = data.content
				this.form.accountId = data.accountId
				this.form.attachId= data.attachId
			}

		},
		addNewFile(res, file)
		{
			let tArr =[];
			if(this.form.accountId!=undefined&&this.form.accountId!='')
			{
				tArr =this.form.attachId.split(",");
			}
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
